package com.jackiechancinema.ssm.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jackiechancinema.ssm.dao.ContractDao;
import com.jackiechancinema.ssm.dao.DailyDao;
import com.jackiechancinema.ssm.dto.ContractExcelDto;
import com.jackiechancinema.ssm.dto.DailyExcelDto;
import com.jackiechancinema.ssm.entity.Contract;
import com.jackiechancinema.ssm.entity.Daily;
import com.jackiechancinema.ssm.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DailyReadListener extends AnalysisEventListener<DailyExcelDto> {
    @Autowired
    private DailyDao dao;

    private List<Daily> list=new ArrayList<>();
    //获取读取到的每一行对应的对象
    @Override
    public void invoke(DailyExcelDto dto, AnalysisContext analysisContext) {
        //实现属性的赋值
        Daily daily= BeanUtil.copyProperty(Daily.class,dto,dto.getClass().getDeclaredFields());
        //添加到集合
        list.add(daily);
        System.err.println(list);
    }
    //整个读取结束
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println(list);
        //将读取的数据一次性的存储到数据库
        int r=dao.insertBatch(list);
        list.clear();
    }
}
